Binarycrossentropygrad ================= 计算二元交叉熵损失函数的梯度。 .. math:: dx_n = dL'_n \cdot w_n \cdot \frac{x_n - y_n}{x_n(1 - x_n) + \epsilon} 其中 :math:`x_n` 是前向预测值, :math:`y_n` 是目标值, :math:`w_n` 是样本权重, :math:`dL'_n` 是上游梯度。`reduction` 参数决定了 :math:`dL'_n` 的广播方式。 输入: - **input_size** - 输入张量的总元素数量。 - **reduction** - 前向传播时使用的规约类型 (0: None, 1: Mean, 2: Sum)。 - **input_x** - 前向传播时的预测值张量。 - **input_y** - 前向传播时的目标值(标签)张量。 - **weight** - (可选) 前向传播时使用的权重张量。 - **dloss** - 来自后一层的上游梯度。 - **dx** - (输出) 梯度结果的存储地址。 - **weight_defined** - 权重是否有效的标志。若为非0,则`weight`参数必须提供。 - **core_mask** - 核掩码。 输出: - **dx** - 写入计算出的对 `input_x` 的梯度。 支持平台: ``FT78NE`` ``MT7004`` .. note:: - FT78NE 支持fp32 - MT7004 支持fp16, fp32 **共享存储版本:** .. c:function:: void fp_binarycrossentropygrad_s(int input_size, int reduction, float* input_x, float* input_y, float* weight, float* dloss, float* dx, int weight_defined, int core_mask) .. c:function:: void hp_binarycrossentropygrad_s(int input_size, int reduction, half* input_x, half* input_y, half* weight, half* dloss, half* dx, int weight_defined, int core_mask) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 16 //FT78NE示例 #include #include int main(int argc, char* argv[]) { float *input_x = (float *)0xA0000000; // forward input_x, DDR float *input_y = (float *)0xB0000000; // forward input_y float *weight = (float *)0xC0000000; // forward weight float *dloss = (float *)0xD0000000; // upstream gradient float *dx = (float *)0xE0000000; // output gradient dx int input_size = 1024; int reduction = 1; // Mean int weight_defined = 1; // true int core_mask = 0xff; fp_binarycrossentropygrad_s(input_size, reduction, input_x, input_y, weight, dloss, dx, weight_defined, core_mask); return 0; } **私有存储版本:** .. c:function:: void fp_binarycrossentropygrad_p(int input_size, int reduction, float* input_x, float* input_y, float* weight, float* dloss, float* dx, int weight_defined) .. c:function:: void hp_binarycrossentropygrad_p(int input_size, int reduction, half* input_x, half* input_y, half* weight, half* dloss, half* dx, int weight_defined) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 15 //FT78NE示例 #include int main(int argc, char* argv[]) { float *input_x = (float *)0x10000000; // forward input_x, L2 float *input_y = (float *)0x11000000; // forward input_y float *weight = (float *)0x12000000; // forward weight float *dloss = (float *)0x13000000; // upstream gradient float *dx = (float *)0x14000000; // output gradient dx int input_size = 1024; int reduction = 1; // Mean int weight_defined = 0; // false fp_binarycrossentropygrad_p(input_size, reduction, input_x, input_y, weight, dloss, dx, weight_defined); return 0; }